/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: January 25 2023
This do file does : 
	Create figures for main text
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off
capture log close


#delim ;

/*-----------------------------------------------------------------------------------
			 Set up
-----------------------------------------------------------------------------------*/
*** Setting up files;
	glo datafile "qu_analysis"; 	// type in the name of the datafile that you want to create;

*** Set up program;
 ** Number Format;
	capture program drop autoformat;
	program define autoformat;

	if int(`1')==`1' global format "%12.0f";
	else if abs(`1')>10 global format "%9.1f";
	else if abs(`1')>1 global format "%9.2f";
	else if abs(`1')>.1 global format "%9.3f";
	else if abs(`1')>.01 global format "%9.3f";
	else if abs(`1')>.001 global format "%9.3f";
	else if abs(`1')>.0001 global format "%9.4f";
	else if abs(`1')<.00001 global format "%9.5f";

	end;
		
	
/*-----------------------------------------------------------------------------------
			 		DATA = FARMER SURVEY
-----------------------------------------------------------------------------------*/			
		
	
*** Read data;
	
	use "${data_path}/FARMER_LONG_SETUP", replace;
	
*** Drop control communes;

	drop if control_commune == 1;	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);

*** Cluster for standard errors;

	egen group_id = group(strata st_tgroup);

*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig;
	gen control = (st_tgroup == 1 | st_tgroup == 5);
	

*** Drop large farms (farm size larger than five hectares);

	drop if ha_whitedragon >= 5;
	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;


	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farm "Farmer Training";
	lab var tr_int "Exporter Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility (C.E.)";
	lab var tr_fXcert "C.E. $\times$ Farmer Training";
	lab var tr_iXcert "C.E. $\times$ Exporter Training";
	lab var tr_jXcert "C.E $\times$ Joint Training";
	lab var std_knowledge "Farmer GAP Knowledge";
	lab var std_perception "Farmer Food Safety Awareness";
	lab var int_std_knowledge "Exporter GAP Knowledge";
	lab var dist_nearest_treated "Distance to nearest jointly trained farmer (km)";
	lab var farmer_in2 "Number of farmers within 2km distance";
	lab var trader_in3 "Number of exporters within 3km distance";
	lab var trader_in2 "Number of exporters within 2km distance";
	lab var dist_jointtrader "Distance to nearest jointly trained exporter (km)";

	
tempfile maindata;
save `maindata';
clear;	


/*-----------------------------------------------------------------------------------
			 Technology Adoption (Figure 4)
-----------------------------------------------------------------------------------*/	

use `maindata';
	
gen traingroup = 1 if st_tgroup == 1 | st_tgroup == 5;	
replace traingroup = 2 if st_tgroup == 2 | st_tgroup == 6;	
replace traingroup = 3 if st_tgroup == 3 | st_tgroup == 7;	
replace traingroup = 4 if st_tgroup == 4 | st_tgroup == 8;	
	
label define grouplab 1 "No training" 2 "Farmer training" 3 "Exporter training" 4 "Joint training";
label values traingroup grouplab;

replace audit_score = audit_score - 0.6;
replace raw_knowledge = raw_knowledge - 4;
replace raw_perception = raw_perception - 3;


label define eligibility2 0 "" 1 "";
label values certelig eligibility2;

cibar std_score if round == 2, over(traingroup certelig) bargap(30) gap(200) baropts(fcolor(none none none none) lwidth(medthick)) ciopts(lcolor(gs4)) graphopts(xline(6.2, lcolor(black)) xlabel(, nolabels) ylabel(-0.5 -0.25 0 0.25 0.5 0.75 1) ytitle("GAP Compliance (Standardized Total Score)") text(0.9 1 "No Certificate Treatment", place (e)) text(0.9 7.5 "Certificate Treatment", place (e)) graphregion(fcolor("white")));
graph export "${figure_path}/cibar_std_score_fu2.png", replace;

cibar mean_std_pest_eu if round == 2, over(traingroup certelig) bargap(30) gap(200) baropts(fcolor(none none none none) lwidth(medthick)) ciopts(lcolor(gs4)) graphopts(xline(6.2, lcolor(black)) xlabel(, nolabels) ylabel(0.5 1 1.5 2) ytitle("Mean Pesticide Residue Level (unit: MRL)") text(1.9 1 "No Certificate Treatment", place (e)) text(1.9 7.5 "Certificate Treatment", place (e)) graphregion(fcolor("white")));
graph export "${figure_path}/cibar_mean_std_pest_eu.png", replace;

est clear;
clear;


/*-----------------------------------------------------------------------------------
			Trading with Exporting Intermediaries by Round (Figure 5)
-----------------------------------------------------------------------------------*/


clear;
	use "${data_path}/FARMER_LONG_SETUP";
	expand 2 if round == 1, gen(baseline);
	replace round = 0 if baseline == 1;
	sort farmerid round;
	tempfile farmer_allround;
	save `farmer_allround';
clear;	
	
*** Read data;
	
	use "${data_path}/FARMER_COMBINETRADE_CLEAN", replace;

	rename followup round;

	
	merge m:1 farmerid round using `farmer_allround';
	

*** Drop control communes;

	drop if control_commune == 1;	

*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);
	
*** Cluster for standard errors;

	egen group_id = group(strata st_tgroup);

*** Drop large farms (farm size larger than five hectares);

	sort farmerid round;
	bys farmerid: egen mean_ha = mean(ha_whitedragon);
	drop if mean_ha >= 5;	

*** Generate treatment levels and interaction terms;

	gen tr_cont = (st_tgroup == 1 | st_tgroup == 5);
	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig;
	gen iv_tr_farmer = (tr_farm == 1 | tr_joint == 1);
	
** Generate outcome variables;

	gen log_buyer_price = log(buyer_price);
	tab buyer_type, gen(dum_type);
	tab buyer_contract, gen(dum_contract);
	
	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var age "Age";
	lab var female "Female";
	lab var edu_second "Secondary Education";
	lab var exp_dragon "Experience growing dragon fruit (years)";
	lab var ha_whitedragon "Size of dragon fruit farm (hectares)";
	lab var tree_whitedragon "Number of dragon fruit trees";
	lab var anycert_base "Received any agricultural certificate before";
	lab var loan_any_base "Ever received loan for farm investment";
	lab var save_bank_base "Ever saved at bank";
	lab var mean_trust_base "Mean score on trust measurement";
	lab var mean_business_base "Mean score on business measurement";
	lab var mean_confidence_base "Mean score on confidence measurement";
	lab var raven_total "Raven matrices score";
	lab var present_bias_base "Time discounting - present biased";
	lab var gap_comply_base "Self-reported GAP compliance";
	lab var dum_market1 "China";
	lab var dum_market2 "Asia (excluding China)";
	lab var dum_market3 "EU/US";
	lab var dum_market4 "Domestic";
	lab var std_score "GAP compliance";
	lab var std_sweet "Sweetness";
	lab var std_skin "Skin";
	lab var std_bract "Bract";
	lab var std_length "Length";
	lab var std_width "Width";
	lab var std_weight "Weight";
	lab var tr_farm "Farmer Training";
	lab var tr_int "Intermediary Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility";
	lab var tr_fXcert " $\times$ Farmer Training";
	lab var tr_iXcert " $\times$ Intermediary Training";
	lab var tr_jXcert " $\times$ Joint Training";
	lab var sum_overmrl_eu "Residue $\geq$ E.U. Limit";
	lab var progmeet "Buyer was program participant";


** Estimation;
	gen quality = .;
	gen qualityXjoint = .;
	gen qualityXint = .;
	gen qualityXfarm = .;
	gen qualityXprogmeet = .;
	gen qualityXcontract = .;
	gen qualityXjointXprogmeet = .;
	gen qualityXjointXnoprog = .;
	gen qualityXjointXcontract = .;
	gen qualityXjointXnocontract = .;
	gen jointXprogmeet = tr_joint*progmeet;
	gen jointXnoprog = tr_joint*(1-progmeet);
	gen jointXcontract = tr_joint*(buyer_contract != 3);
	gen jointXnocontract = tr_joint*(buyer_contract == 3);
	gen havecontract = (buyer_contract !=3);
	lab var quality "Product Quality";
	lab var progmeet "Trade within Cluster";
	lab var qualityXcontract "Product Quality $\times$ Contract";
	lab var qualityXjoint "Product Quality $\times$ Joint Training";
	lab var qualityXint "Product Quality $\times$ Intermediary Training";	
	lab var qualityXfarm "Product Quality $\times$ Farmer Training";		
	lab var qualityXjointXprogmeet "Product Quality $\times$ Joint Training $\times$ Trade within Cluster";	
	lab var qualityXjointXnoprog "Product Quality $\times$ Joint Training $\times$ Trade outside Cluster";
	lab var qualityXjointXcontract "Product Quality $\times$ Joint Training $\times$ Contract";	
	lab var qualityXjointXnocontract "Product Quality $\times$ Joint Training $\times$ No Contract";
	lab var qualityXprogmeet "Product Quality $\times$ Trade within Cluster";
	lab var jointXprogmeet "Joint Training $\times$ Trade within Cluster";
	lab var jointXnoprog "Joint Training $\times$ Trade outside Cluster";
	lab var jointXcontract "Joint Training $\times$ Contract";
	lab var jointXnocontract "Joint Training $\times$ No Contract";	
	lab var havecontract "Contract";
		

*** Contract Trading by Round ***;
	
	gen nonprogmeet = (1-progmeet);
	gen contract_within = (buyer_contract == 1 | buyer_contract == 2)*progmeet ;
	replace contract_within = 0 if buyer_type == 1 ;
	replace contract_within = (buyer_contract==1 | buyer_contract == 2)*exporter if round == 0;	
	gen contract_outside = (buyer_contract == 1 | buyer_contract == 2)*nonprogmeet;
	replace contract_outside = 0 if buyer_type == 1 ;	
	replace contract_outside = (buyer_contract==1 | buyer_contract == 2)*(1-exporter) if round == 0;	
	gen spot_within = (buyer_contract == 3)*progmeet ;
	replace spot_within = (buyer_type == 1)*progmeet ;	
	replace spot_within = (buyer_contract == 3)*exporter if round == 0;	
	gen spot_outside = (buyer_contract == 3)*nonprogmeet ;
	replace spot_outside = (buyer_type == 1)*nonprogmeet ;		
	replace spot_outside = (buyer_contract == 3)*(1-exporter) if round == 0;
	
mat x = J(12,5,.);
local i = 1;
forvalues j = 1(1)4 {;
	mat x[`j',1] = 0;
};

local outcome "contract_within";

	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean';	

	
forvalues j = 5(1)8 {;
	mat x[`j',1] = 1;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
forvalues j = 9(1)12 {;
	mat x[`j',1] = 2;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 2;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
mat colnames x = "round" "group" "mean" "low" "high";	


preserve;
svmat x;
keep x1-x5;
keep if _n <= 12;
rename x1 round;
rename x2 treatment;
rename x3 mean;
rename x4 low;
rename x5 high;

label define roundlab 0 "Baseline" 1 "6 months" 2 "12 months";
label values round roundlab;

twoway (connected mean round if treat == 1, lpattern(solid) lcolor(black) mcolor(black) m(O))  (connected mean round if treat == 2, lpattern("_.") lcolor(edkblue) lwidth(thick) mcolor(edkblue) m(D)) (rcap high low round if treat == 2, lcolor(edkblue)) (connected mean round if treat == 3, lpattern("-..") lcolor(maroon) lwidth(thick) mcolor(maroon) m(T)) (rcap high low round if treat == 3, lcolor(maroon)) (connected mean round if treat == 4, lpattern(shortdash) lcolor(orange) lwidth(thick) mcolor(orange) m(S)) (rcap high low round if treat == 4, lcolor(orange)), graphregion(fcolor("white") margin(r+5)) plotregion(margin(bottom)) ytitle("Percent of trade") ylabel(-0.(0.1)0.5, nogrid) xlabel(0(1)2.2,val nogrid) xtitle("") title("Contract trade within cluster", color(black) size(medium))
legend(size(medsmall) symxsize(8) order(1 2 4 6) lab(1 "Control") lab(2 "Farmer training") lab(4 "Exporter training") lab(6 "Joint training")) saving(`outcome', replace);

	graph export "${figure_path}/figure_`outcome'.png", replace;

restore;

mat x = J(12,5,.);
local i = 1;
forvalues j = 1(1)4 {;
	mat x[`j',1] = 0;
};

local outcome "contract_outside";

	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean';	


forvalues j = 5(1)8 {;
	mat x[`j',1] = 1;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
forvalues j = 9(1)12 {;
	mat x[`j',1] = 2;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 2;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
mat colnames x = "round" "group" "mean" "low" "high";	

preserve;
svmat x;
keep x1-x5;
keep if _n <= 12;
rename x1 round;
rename x2 treatment;
rename x3 mean;
rename x4 low;
rename x5 high;

label define roundlab 0 "Baseline" 1 "6 months" 2 "12 months";
label values round roundlab;

twoway (connected mean round if treat == 1, lpattern(solid) lcolor(black) mcolor(black) m(O))  (connected mean round if treat == 2, lpattern("_.") lcolor(edkblue) lwidth(thick) mcolor(edkblue) m(D)) (rcap high low round if treat == 2, lcolor(edkblue)) (connected mean round if treat == 3, lpattern("-..") lcolor(maroon) lwidth(thick) mcolor(maroon) m(T)) (rcap high low round if treat == 3, lcolor(maroon)) (connected mean round if treat == 4, lpattern(shortdash) lcolor(orange) lwidth(thick) mcolor(orange) m(S)) (rcap high low round if treat == 4, lcolor(orange)), graphregion(fcolor("white") margin(r+5)) plotregion(margin(bottom)) ytitle("Percent of trade") ylabel(-0.(0.1)0.5, nogrid) xlabel(0(1)2.2,val nogrid) xtitle("") title("Contract trade outside cluster", color(black) size(medium))
legend(size(medsmall) symxsize(8) order(1 2 4 6) lab(1 "Control") lab(2 "Farmer training") lab(4 "Exporter training") lab(6 "Joint training")) saving(`outcome', replace);

	graph export "${figure_path}/figure_`outcome'.png", replace;

restore;	
	
mat x = J(12,5,.);
local i = 1;
forvalues j = 1(1)4 {;
	mat x[`j',1] = 0;
};

local outcome "spot_within";

	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean';	
	
forvalues j = 5(1)8 {;
	mat x[`j',1] = 1;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
forvalues j = 9(1)12 {;
	mat x[`j',1] = 2;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 2;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
mat colnames x = "round" "group" "mean" "low" "high";	


preserve;
svmat x;
keep x1-x5;
keep if _n <= 12;
rename x1 round;
rename x2 treatment;
rename x3 mean;
rename x4 low;
rename x5 high;

label define roundlab 0 "Baseline" 1 "6 months" 2 "12 months";
label values round roundlab;

twoway (connected mean round if treat == 1, lpattern(solid) lcolor(black) mcolor(black) m(O))  (connected mean round if treat == 2, lpattern("_.") lcolor(edkblue) lwidth(thick) mcolor(edkblue) m(D)) (rcap high low round if treat == 2, lcolor(edkblue)) (connected mean round if treat == 3, lpattern("-..") lcolor(maroon) lwidth(thick) mcolor(maroon) m(T)) (rcap high low round if treat == 3, lcolor(maroon)) (connected mean round if treat == 4, lpattern(shortdash) lcolor(orange) lwidth(thick) mcolor(orange) m(S)) (rcap high low round if treat == 4, lcolor(orange)), graphregion(fcolor("white") margin(r+5)) plotregion(margin(bottom)) ytitle("Percent of trade") ylabel(0(0.1)0.5, nogrid) xlabel(0(1)2.2,val nogrid) xtitle("") title("Spot trade within cluster", color(black) size(medium))
legend(size(medsmall) symxsize(8) order(1 2 4 6) lab(1 "Control") lab(2 "Farmer training") lab(4 "Exporter training") lab(6 "Joint training")) saving(`outcome', replace);

	graph export "${figure_path}/figure_`outcome'.png", replace;

restore;

mat x = J(12,5,.);
local i = 1;
forvalues j = 1(1)4 {;
	mat x[`j',1] = 0;
};

local outcome "spot_outside";

	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean';
	
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean';	


forvalues j = 5(1)8 {;
	mat x[`j',1] = 1;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 1;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
forvalues j = 9(1)12 {;
	mat x[`j',1] = 2;
};
	eststo: reg `outcome' `treat_coef' `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
	
	sum `outcome' if tr_cont == 1 & round == 2;
	local control_mean = r(mean);
	local i = `i' +1;
	mat x[`i',2] = 1;
	mat x[`i',3] = `control_mean';
	local i = `i'+1;
	mat x[`i',2] = 2;	
	mat x[`i',3] = `control_mean'+_b[tr_farm];
	mat x[`i',4] = `control_mean'+_b[tr_farm] - (1.96*_se[tr_farm]);
	mat x[`i',5] = `control_mean'+_b[tr_farm] + (1.96*_se[tr_farm]);	
	local i = `i'+1;
	mat x[`i',2] = 3;	
	mat x[`i',3] = `control_mean'+_b[tr_int];
	mat x[`i',4] = `control_mean'+_b[tr_int] - (1.96*_se[tr_int]);
	mat x[`i',5] = `control_mean'+_b[tr_int] + (1.96*_se[tr_int]);		
	local i = `i'+1;
	mat x[`i',2] = 4;	
	mat x[`i',3] = `control_mean'+_b[tr_joint];
	mat x[`i',4] = `control_mean'+_b[tr_joint] - (1.96*_se[tr_joint]);
	mat x[`i',5] = `control_mean'+_b[tr_joint] + (1.96*_se[tr_joint]);		
	
mat colnames x = "round" "group" "mean" "low" "high";	

preserve;
svmat x;
keep x1-x5;
keep if _n <= 12;
rename x1 round;
rename x2 treatment;
rename x3 mean;
rename x4 low;
rename x5 high;

label define roundlab 0 "Baseline" 1 "6 months" 2 "12 months";
label values round roundlab;

twoway (connected mean round if treat == 1, lpattern(solid) lcolor(black) mcolor(black) m(O))  (connected mean round if treat == 2, lpattern("_.") lcolor(edkblue) lwidth(thick) mcolor(edkblue) m(D)) (rcap high low round if treat == 2, lcolor(edkblue)) (connected mean round if treat == 3, lpattern("-..") lcolor(maroon) lwidth(thick) mcolor(maroon) m(T)) (rcap high low round if treat == 3, lcolor(maroon)) (connected mean round if treat == 4, lpattern(shortdash) lcolor(orange) lwidth(thick) mcolor(orange) m(S)) (rcap high low round if treat == 4, lcolor(orange)), graphregion(fcolor("white") margin(r+5)) plotregion(margin(bottom)) ytitle("Percent of trade") ylabel(0(0.2)1, nogrid) xlabel(0(1)2.2,val nogrid) xtitle("") title("Spot trade outside cluster", color(black) size(medium))
legend(size(medsmall) symxsize(8) order(1 2 4 6) lab(1 "Control") lab(2 "Farmer training") lab(4 "Exporter training") lab(6 "Joint training")) saving(`outcome', replace);

	graph export "${figure_path}/figure_`outcome'.png", replace;
restore;	

grc1leg spot_within.gph spot_outside.gph contract_within.gph contract_outside.gph , iscale(0.65) pos(6) col(2) graphregion(fcolor("white"));

	graph export "${figure_path}/tradebyround_all_v3.png", replace;


	
	
